export default class CardGrid {
  // grids = {}
  constructor() { }

  render(ctx) {
    const { left, top, itemWidth, itemHeight, rows, cols } = GameGlobal.grid;
    for (let i = 0; i < cols; i++) {
      let l = left + itemWidth * i;
      for (let j = 0; j < rows; j++) {
        let t = top + itemHeight * j;
        // 设置线宽为2px
        ctx.lineWidth = GameGlobal.px2dpr(1);
        ctx.strokeStyle = "red";
        ctx.strokeRect(l, t, itemWidth, itemHeight);
        ctx.stroke();
        /*  this.grids[`${j}-${i}`] = {
          left: l,
          top: t,
          right: l + itemWidth,
          bottom: t + itemHeight,
          with: itemWidth,
          height: itemHeight
        } */
      }
    }
  }
  createData(level) {
    const json = {
      // zIndex: []
    };
    let arr = [];
    for (let i = level; i > 0; i--) {
      this.createLevelData(i, json, arr);
    }

    console.log(json, arr);
    // 控制数量
    arr = arr.splice(0, 30 * GameGlobal.databus.GameIndex);

    // 移除多余卡片，必须是3的倍数
    const extra = arr.length % 3;
    if (extra !== 0) {
      arr.splice(-extra, extra);
    }
    return {
      json,
      arr,
    };
  }
  createLevelData(level, json, list) {
    const { left, top, itemWidth, itemHeight, rows, cols } = GameGlobal.grid;
    // 20 * 20 网格
    const layout = Array.from({ length: rows }, () => Array(cols).fill(0));

    const rowI = rows - 1; //19
    const colJ = rows / 2 - 1; //9
    //根据level确定生成范围, 等级（level）愈大，数量越大，层级越小
    for (let i = level; i < rowI - level; i++) {
      //行范围
      for (let j = 1; j < colJ; j++) {
        const duichencol = 18 - j;
        //列范围
        // 20 % 概率生成卡片，并检查是否有效
        if (Math.random() < 0.1 * level && this.isValid(i, j, layout)) {
          layout[i][j] = 1;
          layout[i][duichencol] = 1;
          const zIndex = 10 - level;
          const w = GameGlobal.card.width;
          const h = GameGlobal.card.height;

          const jsonData = json[zIndex] || (json[zIndex] = []);
          const lef1tPos = left + j * itemWidth;
          const top1Pos = top + i * itemHeight;
          const right1tPos = lef1tPos + w;
          const bottom1Pos = top1Pos + h;

          const lef2tPos = left + duichencol * itemWidth;
          const top2Pos = top + i * itemHeight;
          const right2tPos = lef2tPos + w;
          const bottom2Pos = top2Pos + h;
          const d1 = {
            row: i,
            col: j,
            level: level,
            zIndex: 10 - level,
            left: lef1tPos,
            top: top1Pos,
            right: right1tPos,
            bottom: bottom1Pos,
            index: list.length,
            disabled: false,
          };
          const d2 = {
            row: i,
            col: duichencol,
            level: level,
            zIndex: 10 - level,
            left: lef2tPos,
            top: top2Pos,
            right: right2tPos,
            bottom: bottom2Pos,
            index: list.length + 1,
            disabled: false,
          };
          jsonData.push(d1, d2);
          list.push(d1, d2);
        }
      }
    }
  }

  //检查卡片位置是否有效
  // layout 当前卡片的二维数组(0 空，1己有卡片)
  isValid(row, col, layout) {
    return ![
      layout[row]?.[col] /* 当前 */,
      layout[row][col - 1] /* 左 */,
      layout[row - 1]?.[col - 1] /* 左上 */,
      layout[row - 1]?.[col] /* 上 */,
      layout[row - 1]?.[col + 1] /* 右上 */,
      // layout[row]?.[col + 1],/* 右 */
      // layout[row+1]?.[col],/* 下 */
      // layout[row+1]?.[col+1],/* 右下 */
    ].includes(1);
  }
}
